package com.example.firstopengl;

import android.app.ActionBar;
import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLES20.*;
import android.opengl.GLSurfaceView;

import com.example.firstopengl.objects.Mallet;
import com.example.firstopengl.objects.Table;
import com.example.firstopengl.programs.ColorShaderProgram;
import com.example.firstopengl.programs.TextureShaderProgram;
import com.example.firstopengl.util.ShaderHelper;
import com.example.firstopengl.util.TextureHelper;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.Matrix;

import static com.example.firstopengl.util.Constant.BYTES_PER_FLOAT;
import static com.example.firstopengl.util.ShaderHelper.compileFragmentShader;
import static com.example.firstopengl.util.ShaderHelper.compileVertexShader;

/**
 * Created by liqian-ps on 2017/6/26.
 */

public class AirHotkeyRender implements GLSurfaceView.Renderer {

    private static  final int POSITION_COMPONENT_COUNT = 2;

//    private static final int BYTES_PER_FLOAT = 4;
    private final FloatBuffer  vertexData;
    private Context mContext;

    private static  final String U_COLOR = "u_Color";
    private int uColorLocation;

    private static final String A_POSITION = "a_Position";
    private int aPositionLocation;

    private static final String A_COLOR = "a_Color";
    private int aColorLocation;

    private static final String U_MATRIX = "u_Matrix";
    private int uMatrixLocation;
    private static final int COLOR_COMPONENT_COUNT = 3;
    private static final int STRIDE = (COLOR_COMPONENT_COUNT + POSITION_COMPONENT_COUNT)*BYTES_PER_FLOAT;

    private final float[] projectMatrix = new float[16];

    private Context context;
    private Table table;
    private Mallet mallet;
    private ColorShaderProgram colorShaderProgram;
    private TextureShaderProgram textureShaderProgram;
    private int textureId;

    public AirHotkeyRender(Context context){
        float[] tableVertices = {
//                //triangle 1
//                -0.5f, -0.5f,
//                0.5f, 0.5f,
//                -0.5f, 0.5f,
//
//                //triangle 2
//                -0.5f, -0.5f,
//                0.5f, -0.5f,
//                0.5f, 0.5f,
                // triangle fan
                0f,0f,
                -0.5f, -0.5f,
                0.5f, -0.5f,
                0.5f, 0.5f,
                -0.5f, 0.5f,
                -0.5f, -0.5f,

                //line1
                -0.5f,0f,
                0.5f,0f,

                //malltes
                0f, 0.25f,
                0f, -0.25f,

                //triangle 3
                -0.75f, -0.75f,
                0.75f, 0.75f,
                -0.75f, 0.75f,

                // triangle 4
                -0.75f, -0.75f,
                0.75f, -0.75f,
                0.75f, 0.75f
        };

        float[] tableVerticesWithTriangles = {
                // order   x  y  r g b
                //triangle fan
                0f, 0f,  1f, 1f, 1f,

                -0.5f, -0.8f,  0.7f, 0.7f, 0.7f,
                0.5f, -0.8f,   0.7f, 0.7f, 0.7f,
                0.5f, 0.8f,    0.7f, 0.7f, 0.7f,
                -0.5f, 0.8f,   0.7f, 0.7f, 0.7f,
                -0.5f, -0.8f,  0.7f, 0.7f, 0.7f,

                // line 1
                -0.5f,0f,   1f, 0f, 0f,
                0.5f,0f,    1f, 0f, 0f,

                //malltes
                0f, 0.4f,   0f, 0f , 1f,
                0f, -0.4f,   1f, 0f, 0f,
        };

        vertexData = ByteBuffer.allocateDirect(tableVerticesWithTriangles.length * BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
        vertexData.put(tableVerticesWithTriangles);

        mContext = context;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        table = new Table();
        mallet = new Mallet();

        textureShaderProgram = new TextureShaderProgram(mContext);
        colorShaderProgram = new ColorShaderProgram(mContext);

        textureId = TextureHelper.loadTexture(mContext, R.mipmap.air_hockey_surface);

//        String vertex_shader = TextResourceReader.readTextFileFromResource(mContext, R.raw.simple_vertex_shader);
//        String fragment_shader = TextResourceReader.readTextFileFromResource(mContext, R.raw.simple_fragment_shader);
//
//        int vertexShader = ShaderHelper.compileVertexShader(vertex_shader);
//        int fragmentShader = ShaderHelper.compileFragmentShader(fragment_shader);
//        int programId = ShaderHelper.linkProgram(vertexShader, fragmentShader);
//        ShaderHelper.validateProgram(programId);
//
//        GLES20.glUseProgram(programId);
////        uColorLocation = GLES20.glGetUniformLocation(programId, U_COLOR);
//        aColorLocation = GLES20.glGetAttribLocation(programId, A_COLOR);
//        aPositionLocation = GLES20.glGetAttribLocation(programId, A_POSITION);
//
//        uMatrixLocation = GLES20.glGetUniformLocation(programId, U_MATRIX);
//
//        vertexData.position(0);
//        GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT,GLES20.GL_FLOAT, false, STRIDE ,vertexData );
//        GLES20.glEnableVertexAttribArray(aPositionLocation);
//
//        vertexData.position(POSITION_COMPONENT_COUNT);
//        GLES20.glVertexAttribPointer(aColorLocation, COLOR_COMPONENT_COUNT, GLES20.GL_FLOAT, false, STRIDE, vertexData);
//        GLES20.glEnableVertexAttribArray(aColorLocation);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        float  aspetRatio = width > height ? (float) width / (float)height:(float) height / (float)width;
        if(width > height){
            Matrix.orthoM(projectMatrix, 0, -aspetRatio, aspetRatio, -1f, 1f, -1f, 1f);
        }else {
            Matrix.orthoM(projectMatrix, 0, -1f, 1f, -aspetRatio, aspetRatio, -1f, 1f);
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        textureShaderProgram.useProgram();
        textureShaderProgram.setUniforms(projectMatrix,textureId);
        table.bindData(textureShaderProgram);
        table.draw();

        colorShaderProgram.useProgram();
        colorShaderProgram.setUniforms(projectMatrix);
        mallet.bindData(colorShaderProgram);
        mallet.draw();

//

//        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, projectMatrix, 0);
//
////        GLES20.glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);
//        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);
//
//
////        //draw  line
////        GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
//        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2);
////
////        //draw point
////
////        GLES20.glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
//        GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1);
////
////        //draw point
////        GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 1.0f, 1.0f);
//        GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1);


//        GLES20.glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
//        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 10, 6);
    }
}
